<template>
	<!-- 商品详情优惠券 -->
	<view class="container">
		<headTitle title="选择优惠券"></headTitle>
		<view class="content">
			<coupon v-for="(item, index) in couponList" :key="index" type="use"
				:alldata="item" :index="index" @change="changeStatus"></coupon>
		</view>
	</view>
</template>

<script>
import headTitle from '@/components/head-title'
import coupon from '@/components/coupon'
export default {
	data() {
		return {
			index: 0, // 店铺领取优惠券的索引
			couponList: [], // 数组
		}
	},
	onLoad(opt) {
		this.index = opt.index
		this.loadData()
	},
	methods: {
		// 获取完优惠券改变状态
		changeStatus(obj) {
			this.couponList.map(item => {
				item.couponType = 0
			})
			this.couponList[obj.index].couponType = obj.status
			// 修改确认订单页面的数据id和值
			let data = this.$api.prePage().list[this.index]
			data.couponHistoryId = obj.status ? obj.obj.id : 0
			data.promotionAmount = obj.status ? obj.obj.amount : 0
			this.$api.prePage().calculatePrice()
			uni.navigateBack()
		},
		// 加载数据
		loadData() {
			let data = this.$api.prePage().list
			let i = this.index
			if(i && !isNaN(+i) && data && data[i]) {
				var s = 0
				this.couponList = data[i].couponHistoryDetailList || []
				this.couponList.map(item => {
					if(data[i].couponHistoryId === item.id) {
						s = 1
					}else {
						s = 0
					}
					this.$set(item, 'couponType', s)
					this.$set(item, 'useType', item.userTypeStr)
					item.startTime = item.startTimeStr || ''
					item.endTime = item.endTimeStr || ''
				})
			}
		},
	},
	components: {
		headTitle,
		coupon,
	}
}
</script>

<style lang="scss">
page {
	background-color: #F7F7F7;
}
.content {
	padding: 30rpx 20rpx 0;
}
</style>
